﻿Imports System.Linq
Imports Equipment.Core
Imports Equipment.DataAccess


Public Class AssetManager
    Public Shared Function SearchingEquipmentWrapperByPrefixNameOrID(ByVal prefix As String) As List(Of AssetWrapper)
        Dim result As IQueryable(Of Equipment.DataAccess.Asset)
        If IsNumeric(prefix) Then
            Dim eqList As New List(Of Equipment.DataAccess.Asset)
            Dim eq As Equipment.DataAccess.Asset = Equipment.DataAccess.Asset.GetByPrimaryKey(CInt(prefix))
            If Not eq Is Nothing Then
                eqList.Add(eq)
            End If
            result = eqList.AsQueryable()
        Else
            result = Equipment.DataAccess.Asset.SearchAssetByName(prefix)
        End If

        Return AssetWrapper.ConvertToWrapperList(result)
    End Function

    Public Shared Function GetAllBySiteFor(ByVal forUser As IUserInfo) As IEnumerable(Of Asset)
        If forUser.IsSuperUser Then
            Return Asset.RetrieveAll()
        Else
            Return Asset.RetrieveAllBySiteID(forUser.SiteID)
        End If
    End Function

    Public Shared Function GetAllFor(ByVal forUser As IUserInfo) As IEnumerable(Of Asset)
        If forUser.IsSuperUser Then
            Return Asset.RetrieveAll()
        ElseIf forUser.IsSiteAdmin Then
            Return Asset.RetrieveAllBySiteID(forUser.SiteID)
        Else
            Return Asset.RetrieveAllForGroups(forUser.GroupIDs)
        End If
    End Function

    Public Shared Function GetFilteredFor(ByVal forUser As IUserInfo, ByVal assetTypeID As Integer, ByVal groupID As Integer) As IEnumerable(Of Asset)
        If forUser.IsSuperUser Then
            Return Asset.RetrieveAll(assetTypeID, groupID)
        ElseIf forUser.IsSiteAdmin Then
            Return Asset.RetrieveFilteredBySiteID(forUser.SiteID, assetTypeID, groupID)
        Else
            Return Asset.RetrieveFilteredForGroups(forUser.GroupIDs, assetTypeID, groupID)
        End If
    End Function


    Public Shared Function GetFilteredFor(ByVal forUser As IUserInfo, ByVal assetTypeID As Integer, ByVal siteID As Integer, ByVal departmentID As Integer) As IEnumerable(Of Asset)
        If forUser.IsSuperUser Then
            Return Asset.RetrieveAll(assetTypeID, siteID, departmentID)
        Else
            Return Asset.RetrieveAll(assetTypeID, siteID, departmentID).Where(Function(a) a.SiteDepartment.ParentID = forUser.SiteID)
        End If
    End Function



    Public Shared Function SearchAssetByNameFor(ByVal forUser As IUserInfo, ByVal prefixText As String) As IEnumerable(Of Asset)
        If forUser.IsSuperUser Then
            Return Asset.SearchAssetByName(prefixText)
        ElseIf forUser.IsSiteAdmin Then
            Return Asset.SearchAssetByNameForSiteID(forUser.SiteID, prefixText)
        Else
            Return Asset.SearchAssetByNameForGroups(forUser.GroupIDs, prefixText)
        End If
    End Function

    Public Shared Function SearchAssetByBarCodeFor(ByVal forUser As IUserInfo, ByVal prefixText As String) As IEnumerable(Of Asset)
        If forUser.IsSuperUser Then
            Return Asset.SearchAssetByBarCode(prefixText)
        ElseIf forUser.IsSiteAdmin Then
            Return Asset.SearchAssetByBarCodeForSite(forUser.SiteID, prefixText)
        Else
            Return Asset.SearchAssetByBarCodeForGroups(forUser.GroupIDs, prefixText)
        End If
    End Function


    Public Shared Function GetFiltered(ByVal siteID As Integer, ByVal departmentID As Integer, ByVal assetTypeID As Integer, ByVal groupID As Integer, ByVal assetStatusID As Integer) As IEnumerable(Of Asset)
        Dim assets As IEnumerable(Of Asset)

        If departmentID > 0 Then
            assets = Asset.RetrieveAllByDepartmentID(departmentID, assetTypeID, groupID)
        ElseIf siteID > 0 Then
            assets = Asset.RetrieveAllBySiteID(siteID, assetTypeID, groupID)
        Else
            assets = Asset.RetrieveAll(assetTypeID, groupID)
        End If

        If assetStatusID > 0 Then
            assets = assets.Where(Function(a) a.AssetStatusID = assetStatusID)
        End If

        Return assets
    End Function

End Class
